package org.linlinjava.dumbo.db.service;

import org.apache.ibatis.annotations.Param;
import org.linlinjava.dumbo.db.dao.GoodsProductMapper;
import org.linlinjava.dumbo.db.dao.DumboGoodsProductMapper;
import org.linlinjava.dumbo.db.domain.DumboGoodsProduct;
import org.linlinjava.dumbo.db.domain.DumboGoodsProductExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DumboGoodsProductService {
    @Resource
    private DumboGoodsProductMapper DumboGoodsProductMapper;
    @Resource
    private GoodsProductMapper goodsProductMapper;

    public List<DumboGoodsProduct> queryByGid(Integer gid) {
        DumboGoodsProductExample example = new DumboGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
        return DumboGoodsProductMapper.selectByExample(example);
    }

    public DumboGoodsProduct findById(Integer id) {
        return DumboGoodsProductMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer id) {
        DumboGoodsProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(DumboGoodsProduct goodsProduct) {
        goodsProduct.setAddTime(LocalDateTime.now());
        goodsProduct.setUpdateTime(LocalDateTime.now());
        DumboGoodsProductMapper.insertSelective(goodsProduct);
    }

    public int count() {
        DumboGoodsProductExample example = new DumboGoodsProductExample();
        example.or().andDeletedEqualTo(false);
        return (int) DumboGoodsProductMapper.countByExample(example);
    }

    public void deleteByGid(Integer gid) {
        DumboGoodsProductExample example = new DumboGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid);
        DumboGoodsProductMapper.logicalDeleteByExample(example);
    }

    public int addStock(Integer id, Short num){
        return goodsProductMapper.addStock(id, num);
    }

    public int reduceStock(Integer id, Short num){
        return goodsProductMapper.reduceStock(id, num);
    }

    public void updateById(DumboGoodsProduct product) {
        product.setUpdateTime(LocalDateTime.now());
        DumboGoodsProductMapper.updateByPrimaryKeySelective(product);
    }
}